package com.tg.message.msg;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.base.BaseActivity;
import com.demo.view.ScaleGestureImageView;
import com.magicindicator.buildins.UIUtil;
import com.tange.base.toolkit.StatusBarUtil;
import com.tange.base.toolkit.StatusNavUtils;
import com.tange.core.backend.service.ep.EnvironmentProxy;
import com.tg.appcommon.android.FixMemLeak;
import com.tg.appcommon.android.ImageUtils;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.message.R;

public class MessageFullScreenActivity extends BaseActivity {
    public static final String TAG_DEVICE_NAME = "MessageFullScreenActivity#Name";
    public static final String TAG_IMAGE_PATH = "MessageFullScreenActivity#Image";
    private ImageView cover;


    public static long pageStart;

    private Activity activity;
    private String url, name;
    private View rootView;

    public void setActivity(Activity activity){
        this.activity = activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
        pageStart = System.currentTimeMillis();
        url = getIntent().getStringExtra(TAG_IMAGE_PATH);
        name = getIntent().getStringExtra(TAG_DEVICE_NAME);
        TGLog.d("MessageFullScreenActivity", "url = " + url);
        initInternalView(savedInstanceState);
        fillPage();
    }

    private void initWindow() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.layout_fullscreen_player);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        window.getDecorView().setBackgroundColor(Color.BLACK);
        hideActionBar();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected int statusBarColor() {
        return Color.BLACK;
    }

    @Override
    protected void initView() {
    }

    private void initInternalView(Bundle savedInstanceState) {
        cover = findViewById(R.id.cover);
        TextView textView = findViewById(R.id.title_tv);
        rootView = findViewById(R.id.message_play_layout_inside);
        textView.setText(name);
        findViewById(R.id.back_player).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

        EnvironmentProxy.initStatic();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        FixMemLeak.fixLeak(this);
    }

    private void saveLayoutParams( RelativeLayout.LayoutParams oldParams,  RelativeLayout.LayoutParams newParams){
        newParams.width = oldParams.width;  // 重新设置属性值
        newParams.height = oldParams.height;
        newParams.setMarginStart(oldParams.getMarginStart());
        newParams.topMargin = oldParams.topMargin;
    }

    private void resizeRootView() {
        rootView.post(() -> {
            final int width = UIUtil.getScreenWidth(getApplication());
            RelativeLayout.LayoutParams lpCover = (RelativeLayout.LayoutParams)cover.getLayoutParams();
            RelativeLayout.LayoutParams fullImageLayoutParams = new RelativeLayout.LayoutParams(lpCover);
            saveLayoutParams(lpCover, fullImageLayoutParams);
            lpCover.width = width;
            lpCover.height = ViewGroup.LayoutParams.MATCH_PARENT;
            lpCover.addRule(Gravity.CENTER);
            lpCover.setLayoutDirection(Gravity.CENTER);
            cover.setScaleType(ImageView.ScaleType.FIT_CENTER);
            cover.setLayoutParams(lpCover);

        });
    }

    private void showCover() {
        if (cover instanceof ScaleGestureImageView){
            ((ScaleGestureImageView)cover).setCanScale(true);
        }
        ImageUtils.loadImage(this, cover, url, false, false, bitmap -> {
            cover.setVisibility(View.VISIBLE);
            cover.setImageBitmap(bitmap);
            resizeRootView();
        });

        int statusBarHeight = (int) ResourcesUtil.getResources().getDimension(R.dimen.status_bar_height);
        int resourceId =  ResourcesUtil.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = ResourcesUtil.getResources().getDimensionPixelSize(resourceId);
        }

        View view = findViewById(R.id.player_title_view);
        RelativeLayout.LayoutParams flp = (RelativeLayout.LayoutParams) view.getLayoutParams();
        flp.topMargin = statusBarHeight;
        view.setLayoutParams(flp);

    }

    public void refresh() {
        showCover();
    }

    private void fillPage() {
        if (isDestroyed() || isFinishing()) {
            return;
        }
        refresh();
        StatusBarUtil.setLightStatusBar(this, true);
        StatusNavUtils.setStatusBarColor(this, getResources().getColor(R.color.black));
    }
}
